﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceProcess;
using System.Drawing;

namespace MutliServiceController
{
    public class ServiceMenuItem : ToolStripMenuItem
    {
        private ToolStripMenuItem StartServiceMenu;
        private ToolStripMenuItem StopServiceMenu;
        private ToolStripMenuItem ResetServiceMenu;
        private ServiceItem serviceItem;
        public delegate void StatusChangedInvoker(ServiceMenuItem sender, ServiceControllerStatus newStatus);
        public event StatusChangedInvoker OnStatusChanged;

        public ServiceMenuItem (ServiceItem item) : base(item.Name) 
        {
            this.serviceItem = item;
            this.serviceItem.MenuItem = this;
            serviceItem.OnStatusChanged += new ServiceItem.OnStatusChanged_Handler(serviceItem_OnStatusChanged);
            StartServiceMenu = new ToolStripMenuItem("Start");
            StartServiceMenu.Click += new EventHandler(StartServiceMenu_Click);
            StopServiceMenu = new ToolStripMenuItem("Stop");
            StopServiceMenu.Click += new EventHandler(StopServiceMenu_Click);
            ResetServiceMenu = new ToolStripMenuItem("Reset");
            ResetServiceMenu.Click += new EventHandler(ResetServiceMenu_Click);
            this.DropDownItems.Add(StartServiceMenu);
            this.DropDownItems.Add(StopServiceMenu);
            this.DropDownItems.Add(ResetServiceMenu);
        }

        void StartServiceMenu_Click(object sender, EventArgs e)
        {
            serviceItem.Start();
        }

        void StopServiceMenu_Click(object sender, EventArgs e)
        {
            serviceItem.Stop();
        }

        void ResetServiceMenu_Click(object sender, EventArgs e)
        {
            serviceItem.Reset();
        }

        public void UpdateStatus()
        {
            UpdateStatus(this.serviceItem.Status);
        }

        public void UpdateStatus(ServiceControllerStatus newStatus)
        {
            if (this.serviceItem.Available)
            {
                StartServiceMenu.Enabled = (newStatus == ServiceControllerStatus.Stopped);
                StopServiceMenu.Enabled = (newStatus == ServiceControllerStatus.Running);
                ResetServiceMenu.Enabled = (newStatus == ServiceControllerStatus.Running);
                switch (newStatus)
                {
                    case ServiceControllerStatus.Running:
                        this.Image = new Bitmap(@"Images\green.jpg");
                        break;
                    case ServiceControllerStatus.Stopped:
                        this.Image = new Bitmap(@"Images\red.jpg");
                        break;
                    default:
                        this.Image = new Bitmap(@"Images\yellow.jpg");
                        break;
                }
            }
            else
            {
                this.Text = this.serviceItem.Name + " - Unavailable";
                this.Image = null;
            }
        }

        void serviceItem_OnStatusChanged(ServiceItem item, ServiceControllerStatus status)
        {
            if (OnStatusChanged != null)
            {
                OnStatusChanged(this, status);
            }
        }


    }
}
